package fiuba.algo3.algothief.modelo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class ListaLadrones {
	
	public ListaLadrones(){}
	
	public void setearLadrones(ArrayList<Ladron> lista) throws ParserConfigurationException, TransformerException, SAXException, IOException{
		lista.add(new Ladron("Nick Brunch","Masculino","Alpinismo","Negro","Anillo","Moto", new ObjetoRobadoComun("televisor")));
		lista.add(new Ladron("Len Bulk","Masculino","Alpinismo","Rojo","Tatuaje","Descapotable",new ObjetoRobadoComun("heladera")));
		lista.add(new Ladron("Ihor Ihorovitch","Masculino","Croquet","Rubio","Tatuaje","Limusina",new ObjetoRobadoComun("computadora")));
		lista.add(new Ladron("Fast Eddie B.","Masculino","Croquet","Negro","Joyas","Descapotable",new ObjetoRobadoComun("pen drive")));
		lista.add(new Ladron("Scar Graynolt","Masculino","Croquet","Rojo","Anillo","Limusina",new ObjetoRobadoValioso("dolares")));
		lista.add(new Ladron("Merey Laroc","Femenino","Alpinismo","Castanio","Joyas","Limusina",new ObjetoRobadoValioso("euros")));
		lista.add(new Ladron("Lady Agatha","Femenino","Tenis","Rojo","Anillo","Descapotable",new ObjetoRobadoValioso("autos")));
		lista.add(new Ladron("Katherine Drib","Femenino","Alpinismo","Castanio","Tatuaje","Moto",new ObjetoRobadoMuyValioso("collar")));
		lista.add(new Ladron("Dazzle Annie","Femenino","Tenis","Rubio","Tatuaje","Limusina",new ObjetoRobadoMuyValioso("joyas")));
		lista.add(new Ladron("Carmen Sandiego","Femenino","Tenis","Castanio","Joyas","Descapotable",new ObjetoRobadoMuyValioso("diamantes")));
	}

	public Ladron ladronAAtrapar(Policia unPolicia) throws ParserConfigurationException, TransformerException, SAXException, IOException {
		Ladron unLadron = unPolicia.atraparAlLadron();
		return (unLadron);
	}
	
	public void generarXML(ArrayList<Ladron> ladrones) throws ParserConfigurationException, TransformerException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.newDocument();
		Element elementoLadrones = doc.createElement("Ladrones");
		doc.appendChild(elementoLadrones);
		String nombreArchivo;
		nombreArchivo = "ladrones.xml";
		this.setearLadrones(ladrones);
		int tamanioLista = ladrones.size();
		for(int i=0; i<tamanioLista; i++) {
			
			Element ladronSerializado = (ladrones.get(i)).serializar(doc);
			elementoLadrones.appendChild(ladronSerializado);
		}
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		File archivoDestino = new File(nombreArchivo);
		StreamResult result = new StreamResult(archivoDestino);
		transformer.transform(source, result);
		
	}	

}
